package cn.snowtech.bluebox.controller;

import cn.snowtech.bluebox.domain.Result;
import cn.snowtech.bluebox.dto.remind.ReminderRequestDTO;
import cn.snowtech.bluebox.dto.remind.ReminderResponseDTO;
import cn.snowtech.bluebox.service.ReminderService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 提醒事项控制器.
 */
@RestController
public class RemindController {
    private final static Logger loger= LoggerFactory.getLogger(RemindController.class);

    @Autowired
    ReminderService reminderService;

    /**
     * 创建提醒事项的方法
     * @param reminder 提醒事项数据
     * @return 保存提醒事项数据的结果
     */
    @RequestMapping(value = "createReminder",consumes = "application/json",method = RequestMethod.POST)
    private ReminderResponseDTO createReminder(@RequestBody String reminder){
        ObjectMapper mapper=new ObjectMapper();
        ReminderRequestDTO requestDTO=null;
        ReminderResponseDTO responseDTO=new ReminderResponseDTO();

        try {
            requestDTO=mapper.readValue(reminder,ReminderRequestDTO.class);
        } catch (IOException e) {
            if (loger.isDebugEnabled()){
                loger.debug(e.getMessage());
            }
            responseDTO.setResult(Result.R1002.getCode());
            responseDTO.setMessage(Result.R1002.getMessage());
            return responseDTO;
        }
        return reminderService.create(requestDTO);
    }

    @RequestMapping("/")
    private String index(){

        return "index.html";
    }
}
